// 跟用户相关的所有请求
import { request } from "@/utils"

// 获取文章频道
export const getChannelsApi = () => { 
  return request({ 
    url: '/channels', 
  })
}
// 发布文章
export const createArticleApi = (form: { 
  title: string, channel_id: number, content: string, 
  cover: { type: number, images: string[] }
}) => {
  return request({ 
    url: '/mp/articles?draft=false', 
    method: "POST",
    data: form
  })
}

export interface FilterArticleParams {
  page: number,
  per_page: number,
  begin_pubdate: null | string,
  end_pubdate: null | string,
  status: null | number,
  channel_id: null | string
}
export interface Article {
  id: string,
  comment_count: number,
  cover: {
    images: any[],
  },
  like_count: number,
  pubdate: string,
  read_count: number,
  status: number,
  title: string
}
/* 
  // 表格数据
  const data = [
    {
      id: '8218',
      comment_count: 0,
      cover: {
        images: [],
      },
      like_count: 0,
      pubdate: '2019-03-11 09:00:00',
      read_count: 2,
      status: 2,
      title: 'wkwebview离线化加载h5资源解决方案'
    }
  ]
*/
// 根据id获取文章
export const getArticleByIdApi = (articleId: string) => {
  return request.get('/mp/articles/' + articleId)
}
// 筛选获取文章
export const getArticleApi = (params: FilterArticleParams) => {
  return request.get('/mp/articles', { params })
}

// 修改文章
export const updateArticleApi = (
  articleId: string, 
  data: { 
    title: string, channel_id: number, content: string, 
    cover: { type: number, images: string[] }
  }
) => {
  return request.put(`/mp/articles/${articleId}?draft=false`, data)
}

// 删除文章
export const delArticleApi = (id: string) => {
  return request.delete(`/mp/articles/${id}`)
}